package com.yuqing.sentimentapp.workspace.springfire;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.linkface.baselibrary.http.BaseBean;
import com.yuqing.sentimentapp.R;
import com.yuqing.sentimentapp.base.BaseActivity;
import com.yuqing.sentimentapp.base.BaseObserver;
import com.yuqing.sentimentapp.module.ModuleManager;
import com.yuqing.sentimentapp.workspace.springfire.entity.SpringFireInfoResult;
import com.yuqing.sentimentapp.workspace.springfire.entity.SpringFireReportResult;
import com.yuqing.sentimentapp.workspace.week.entity.WeekPlanInfoResult;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * 台风抢险
 * Created by shenhe on 2020/4/3.
 */
public class SpringFireFillInActivity extends BaseActivity {

    @BindView(R.id.iv_back)
    ImageView mIvBack;
    @BindView(R.id.tv_save)
    TextView mTvSave;
    @BindView(R.id.et_fireCount)
    EditText mEtFireCount;
    @BindView(R.id.et_actionCount)
    EditText mEtActionCount;
    @BindView(R.id.et_carCount)
    EditText mEtCarCount;
    @BindView(R.id.et_firemanCount)
    EditText mEtFiremanCount;


    private boolean editEnable = true;
    private SpringFireInfoResult.DataBean actionInfo;
    private SpringFireReportResult.DataBean.SubSumBean subSum;
    /**
     * 从已上报列表跳转过来的
     */
    private SpringFireReportResult.DataBean.ReportsBean reportsInfo;


    public static void goActivity(Activity activity, SpringFireInfoResult.DataBean actionInfo, SpringFireReportResult.DataBean.SubSumBean subSum) {
        Intent intent = new Intent(activity, SpringFireFillInActivity.class);
        intent.putExtra("actionInfo", actionInfo);
        intent.putExtra("subSum", subSum);
        activity.startActivity(intent);
    }

    public static void goActivity(Activity activity, SpringFireReportResult.DataBean.ReportsBean reportsInfo) {
        Intent intent = new Intent(activity, SpringFireFillInActivity.class);
        intent.putExtra("reportsInfo", reportsInfo);
        activity.startActivity(intent);
    }


    @Override
    public int getLayoutId() {
        return R.layout.activity_spring_fire_fill_in;
    }

    @Override
    public void initPresenter() {
        Intent intent = getIntent();
        if (intent == null) return;
        actionInfo = ((SpringFireInfoResult.DataBean) intent.getSerializableExtra("actionInfo"));
        subSum = ((SpringFireReportResult.DataBean.SubSumBean) intent.getSerializableExtra("subSum"));

        reportsInfo = ((SpringFireReportResult.DataBean.ReportsBean) intent.getSerializableExtra("reportsInfo"));

    }

    @Override
    public void initView() {
        if (actionInfo != null) {
            mEtFireCount.setText(actionInfo.getFireCount());
            mEtActionCount.setText(actionInfo.getActionCount());
            mEtCarCount.setText(actionInfo.getCarCount());
            mEtFiremanCount.setText(actionInfo.getFiremanCount());
        } else {
            if (subSum != null) {
                mEtFireCount.setText(subSum.getFireCount());
                mEtActionCount.setText(subSum.getActionCount());
                mEtCarCount.setText(subSum.getCarCount());
                mEtFiremanCount.setText(subSum.getFiremanCount());
            }
        }

        /**
         * 从已上报列表跳转过来的，不可编辑，隐藏保存按钮
         */
        if (reportsInfo != null) {
            mEtFireCount.setText(reportsInfo.getFireCount());
            mEtActionCount.setText(reportsInfo.getActionCount());
            mEtCarCount.setText(reportsInfo.getCarCount());
            mEtFiremanCount.setText(reportsInfo.getFiremanCount());

            mEtFireCount.setEnabled(false);
            mEtActionCount.setEnabled(false);
            mEtCarCount.setEnabled(false);
            mEtFiremanCount.setEnabled(false);

            mTvSave.setVisibility(View.GONE);
        }
    }

    @OnClick({R.id.iv_back, R.id.tv_save})
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_save:
                if (actionInfo == null) {
                    actionInfo = new SpringFireInfoResult.DataBean();
                    setData();
                    saveReportInfo(actionInfo);
                } else {
                    setData();
                    updateReportInfo(actionInfo);
                }
                break;
        }
    }

    private void setData() {
        actionInfo.setFireCount(TextUtils.isEmpty(mEtFireCount.getText().toString()) ? "0" : mEtFireCount.getText().toString());
        actionInfo.setActionCount(TextUtils.isEmpty(mEtActionCount.getText().toString()) ? "0" : mEtActionCount.getText().toString());
        actionInfo.setCarCount(TextUtils.isEmpty(mEtCarCount.getText().toString()) ? "0" : mEtCarCount.getText().toString());
        actionInfo.setFiremanCount(TextUtils.isEmpty(mEtFiremanCount.getText().toString()) ? "0" : mEtFiremanCount.getText().toString());
    }

    private void updateReportInfo(SpringFireInfoResult.DataBean actionInfo) {
        ModuleManager.getInstance().updateSpringFireReportInfoResult(actionInfo)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseBean>() {
                    @Override
                    protected void hideDialog() {
                        stopProgressDialog();
                    }

                    @Override
                    protected void showDialog() {
                        startProgressDialog();
                    }

                    @Override
                    protected void onSuccess(BaseBean baseBean) {
                        showShortToast("保存成功");
                        finish();
                    }

                    @Override
                    protected void onFailure(String message) {
                        showShortToast(message);
                    }
                });
    }

    private void saveReportInfo(SpringFireInfoResult.DataBean actionInfo) {
        ModuleManager.getInstance().saveSpringFireReportInfoResult(actionInfo)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseBean>() {
                    @Override
                    protected void hideDialog() {
                        stopProgressDialog();
                    }

                    @Override
                    protected void showDialog() {
                        startProgressDialog();
                    }

                    @Override
                    protected void onSuccess(BaseBean baseBean) {
                        showShortToast("保存成功");
                        finish();
                    }

                    @Override
                    protected void onFailure(String message) {
                        showShortToast(message);
                    }
                });
    }
}
